import java.io.*;
import java.net.InetAddress;
import java.net.Socket;


public class MyFile {
    public static void main(String[] args) throws IOException {
        String portString = "8888";
        if (args.length != 0)
            portString = args[0];
        int port = Integer.parseInt(portString);

        //首页
        getSource("/WebContent/all.html", port, "MyServerTest");
        //Test for files list
        getSource("/", port, "MyServerTest");
        //Test for Servlet
        getSource("/WebContent/test1.html", port, "MyServerTest");
        postSource("/WebContent/PetServlet", port, "MyServerTest", "legs=4&weight=10");
        //Test for Simple Jsp
        getSource("/WebContent/test2.jsp", port, "MyServerTest");
        //Test for Servlet MVC
        getSource("/WebContent/test3.html", port, "MyServerTest");
        postSource("/WebContent/PetServlet2", port, "MyServerTest", "legs=4&weight=10");
        //Test for Filter
        getSource("/WebContent/show5.jsp", port, "MyServerTest");
        //Test for Session
        getSource("/WebContent/test6.jsp", port, "MyServerTest");
        postSource("/WebContent/Login", port, "MyServerTest", "username=admin&password=admin");
        //输出对比结果
        ClosestCodeMatches.getRes();
    }

    public static void getTomcat() throws IOException {
        //爬取tomcat
        //首页
        int port=8080;
        getSource("/all.html", port, "TomcatTest");
        //Test for files list
        //getSource("/", port, "TomcatTest");
        //Test for Servlet
        getSource("/test1.html", port, "TomcatTest");
        postSource("/PetServlet", port, "TomcatTest", "legs=4&weight=10");
        //Test for Simple Jsp
        getSource("/test2.jsp", port, "TomcatTest");
        //Test for Servlet MVC
        getSource("/test3.html", port, "TomcatTest");
        postSource("/PetServlet2", port, "TomcatTest", "legs=4&weight=10");
        //Test for Filter
        getSource("/show5.jsp", port, "TomcatTest");
        //Test for Session
        getSource("/test6.jsp", port, "TomcatTest");
        postSource("/Login", port, "TomcatTest", "username=admin&password=admin");
        getSource("/test6.jsp", port, "TomcatTest",true);
    }


    public static void getSource(String netAddress, int port, String place) throws IOException {
        Socket socket = new Socket(InetAddress.getByName("localhost"), port);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        bw.write("GET " + netAddress + " HTTP/1.1\r\n");
        bw.write("Host: localhost\r\n");
        bw.write("Connection: Keep-Alive\r\n\r\n");
        bw.flush();
        socket.shutdownOutput();

        netAddress = netAddress.replace("/", "_");
        netAddress = netAddress.replace("\\", "_");
        netAddress = netAddress.replace(".", "_");
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("MyWebTester\\" + place + "\\" + netAddress + ".txt"))));
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str = null;


        int tag = 0;
        while ((str = br.readLine()) != null) {
            tag++;
            if(tag<=3)
                continue;
            writer.write(str + "\n");
        }

        writer.close();
        br.close();
        bw.close();
        socket.close();
    }

    public static void postSource(String netAddress, int port, String place, String paramter) throws IOException {
        Socket socket = new Socket(InetAddress.getByName("localhost"), port);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        bw.write("POST " + netAddress + " HTTP/1.1\r\n");
        bw.write("Host: localhost\r\n");
        bw.write("Content-Length: " + paramter.length() + "\r\n");
        bw.write("Content-Type: application/x-www-form-urlencoded\r\n");
        bw.write("Connection: Keep-Alive\r\n\r\n");

        bw.write(paramter);//legs=4&weight=10
//        System.out.println(paramter);

        bw.flush();
        socket.shutdownOutput();

        netAddress = netAddress.replace("/", "_");
        netAddress = netAddress.replace("\\", "_");
        netAddress = netAddress.replace(".", "_");
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("MyWebTester\\" + place + "\\" + netAddress + ".txt"))));
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str = null;

        int tag = 0;
        while ((str = br.readLine()) != null) {
            tag++;
            if(tag<=3)
                continue;
            writer.write(str + "\n");
        }


        writer.close();
        br.close();
        bw.close();
        socket.close();
    }

    public static void getSource(String netAddress, int port, String place,boolean isRedirect) throws IOException {
        Socket socket = new Socket(InetAddress.getByName("localhost"), port);

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        bw.write("GET " + netAddress + " HTTP/1.1\r\n");
        bw.write("Host: localhost\r\n");
        bw.write("Connection: Keep-Alive\r\n\r\n");
        bw.flush();
        socket.shutdownOutput();

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("MyWebTester\\" + place + "\\" + "_Login" + ".txt"))));
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str = null;


        int tag = 0;
        while ((str = br.readLine()) != null) {
            tag++;
            if(tag<=9)
                continue;
            writer.write(str + "\n");
        }

        writer.close();
        br.close();
        bw.close();
        socket.close();
    }
}

